package com.letv.android.client.view;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ProgressBar;

/**
 * 用作WebView中进度条显示
 * Created by liuxx on 14-8-27.
 */
public class NetProgressBar extends ProgressBar {
    private int connectNetProgress = 0;
    private ScheduledExecutorService service;
    private boolean isShowingNetProgress = false;

    public NetProgressBar(Context context) {
        super(context);
    }

    public NetProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NetProgressBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void startNetConnectionProgress() {
//        if (!service.isTerminated()){
//            return;
//        }
        isShowingNetProgress = true;
        service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (connectNetProgress > 50) {
                    service.shutdownNow();
                    return;
                }
                NetProgressBar.this.setProgress(connectNetProgress);
                connectNetProgress += 1;
            }
        }, 0, 40, TimeUnit.MILLISECONDS);
    }

    public int getNetConnectionProgress() {
        return connectNetProgress;
    }

    public void setConnectNetProgress(int progress) {
        this.connectNetProgress = progress;
    }

    public void clearNetConnectProgress() {
        this.connectNetProgress = 0;
    }

    public void shutdownNetProgressNow() {
        if (service != null) {
            service.shutdownNow();
        }
    }
}
